package NewTmas.Listeners;


import taskmaster.job.Task;
import taskmaster.job.TaskCollection;
import taskmaster.util.Global;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 8/5/11
 * Time: 1:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class ViewSuccPredTaskListener implements ActionListener {

    TaskCollection taskCollection;
    Task task;
    JDesktopPane dtp;
    Task[] arr;
    JInternalFrame frame=new JInternalFrame();

    public ViewSuccPredTaskListener(Task t, TaskCollection taskCollection, String jobTag, JDesktopPane dtp) {
        this.taskCollection = taskCollection;
        this.task = t;
        this.jobTag = jobTag;
        this.dtp=dtp;
    }

    String jobTag;


    public void actionPerformed(ActionEvent e) {
        JTable tab=new JTable();
        Object rows[][]=null;
        Object headers[]={"Job","Task","Status"};
        if(((JButton)e.getSource()).getText().equalsIgnoreCase("View Successors"))
        {

            frame.setTitle("SUCCESSORS :"+task.getTaskTag()+":"+jobTag);

            try {
                arr=taskCollection.getAllDescendants(task,false,false,true);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
            rows=new Object[arr.length][3] ;

            for(int i=0;i<arr.length;i++)
            {
                rows[i][0]=jobTag;
                rows[i][1]=  arr [i].getTaskTag();
                rows[i][2]= Global.statusNames.get(arr[i].getStatusId());

            }

        }
        else{
                 frame.setTitle("SUCCESSORS :"+task.getTaskTag()+":"+jobTag);

            try {
               arr= taskCollection.getWaitingFor(task,true,true);
            } catch (Exception e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
             rows=new Object[arr.length][3] ;
            for(int i=0;i<arr.length;i++)
            {
                rows[i][0]=jobTag;
                rows[i][1]=  arr [i].getTaskTag();
                rows[i][2]= Global.statusNames.get(arr[i].getStatusId());

            }

        }

        TableModel model=new DefaultTableModel(rows,headers);
        tab.setModel(model);
        frame.add(new JScrollPane(tab));
        frame.setIconifiable(true);
        frame.setMaximizable(true);
        frame.setClosable(true);
        frame.setResizable(true );
        frame.setSize(300,300);
        dtp.add(frame);
        frame.setVisible(true);












    }










}



